﻿/// <reference path="Z:\SoProject\LiberUI/jquery-vsdoc.js" />
;CRM = { IE: 11};

CRM.PreLoad = function () {
    if (typeof jQuery === 'undefined') {
        var jq = document.createElement("script");
        jq.setAttribute("src", 'http://localhost/SiteUI/MiniCRM/jq1.7.js');
        jq.setAttribute('async', 'false');
        window.top.document.getElementsByTagName('head')[0].appendChild(jq);
    };
    setTimeout(CRM.Load, 5000);
};

//在多个框架集环境中，似乎明源切换模块时不会第二次触发onload，操蛋啊
window.top.attachEvent('onload', CRM.PreLoad);

CRM.Load = function () {
    //判断是否已经加载
    if (typeof CRM.Bar !== 'undefined') {
        return;
    }
    //TODO:判断当前页面是否能加载
    CRM.Host = window.top.frames['stage'];
    if (typeof CRM.Host !== 'undefined') {
        CRM.Host = CRM.Host.document.getElementsByTagName('body')[0];
    }
    else{
        //如果没有stage框架
        var bodys = document.getElementsByTagName('body');
        if (bodys.length == 0) {
            return;
        }
        else {
            //尝试找到足够大的body
            for (var i = 0; i < bodys.length; i++) {
                var w = bodys[i].clientWidth;
                var h = bodys[i].clientHeight;
                if (w > 400 && h > 400) {
                    CRM.Host = bodys[i];
                    break;
                }
            }
        }
    }
    if (typeof CRM.Host=='undefined') {
        return;
    }
    if ($(CRM.Host).children("#MiniCrmBar").length > 0) {
        return;
    }
    //TODO:登录验证第三方系统是否已注册，并绑定到SoProject中的某个帐号
    //通过Cookie获取ERP中用户名
    console.log(document.cookie);
    var ck = "ERPUser";
    var ckValue;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = $.trim(cookies[i]);
            if (cookie.substring(0, ck.length) == ck) {
                ckValue =cookie.substring(ck.length);
                break;
            }
        }
    }
    console.log(decodeURIComponent(ckValue));
    $.ajax({
        url: "http://localhost/CRM/AppLogin",
        type: "GET",
        dataType: 'jsonp',
        jsonp: 'callback',
        jsonpCallback: 'onLoginEnd',
        data: { app: 'myerp-agile', appKey: '123' }
    });

    //由于明源ERP切换模块时不再触发onload，只好定时循环加载了
    setTimeout(CRM.Load, 15000);
};

onLoginEnd = function (rtn) {
    if (rtn.IsPositive == true&&typeof CRM.Bar == 'undefined') {
        //判断登录结果，记录身份凭据用以后继操作
        CRM.UId = rtn.Data.UIdStr;
        CRM.UToken = rtn.Data.UToken;

        //添加样式引用
        var sy = document.createElement('link');
        sy.setAttribute('href', 'http://localhost/SiteUI/MiniCRM/myerpCRM.css');
        sy.setAttribute('type', 'text/css');
        sy.setAttribute('rel', 'stylesheet');

        CRM.Host.document.getElementsByTagName('head')[0].appendChild(sy);

        //创建工具条
        var bar = document.createElement('div');
        CRM.Bar = $(bar).attr('id', 'MiniCrmBar');
        CRM.Host.appendChild(bar);

        //TODO:判断IE版本，决定是否需要用js控制工具条的位置和透明度
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("msie") > 0) {
            if (ua.indexOf("msie 6.0") > 0) {
                //ie6
                CRM.IE = 6;
                CRM.Bar.on('mouseenter', AsHover).on('mouseleave', AsLeave);
            }
            else if (ua.indexOf("msie 7.0") > 0) {
                //ie7
                CRM.IE = 7;
                CRM.Bar.on('mouseenter', AsHover).on('mouseleave', AsLeave);
            }
            else if (ua.indexOf("msie 8.0") > 0) {
                //ie8
                CRM.IE = 8;
                CRM.Bar.on('mouseenter', AsHover).on('mouseleave', AsLeave);
            }
            else if (ua.indexOf("msie 9.0") > 0) {
                //ie9
                CRM.IE = 9;
                CRM.Bar.on('mouseenter', AsHover).on('mouseleave', AsLeave);
            }
            else if (ua.indexOf("msie 10.0") > 0) {
                //ie10
                CRM.IE = 10;
                CRM.Bar.on('mouseenter', AsHover).on('mouseleave', AsLeave);
            }
        }
        $.ajax({
            url: "http://localhost/CRM/Bar",
            type: "GET",
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'LoadMenu',
            data: { appUser: 'admin', uid: CRM.UId, uToken: CRM.UToken }
        });
    }
    else {
        
    }
};

LoadMenu = function (rtn) {
    if (rtn.IsPositive) {
        var html = unescape(rtn.Data);
        CRM.Bar.html(html);
        if (CRM && CRM.IE && CRM.IE < 8) {
            $(CRM.Bar).find("#CRMMenu>li").on('mouseenter', AsHoverItem).on('mouseleave', AsLeaveItem).on('click', ItemClick);
        };
    }
};

function ItemClick() {
    var lnk = $(this).attr('Link') + '?uid=' + CRM.UId + '&utoken=' + CRM.UToken+'&random='+Math.random;
    window.showModelessDialog(lnk, null, "dialogHeight:400px,dialogWidth:600px,scroll:1,status:1");
};

function AsHover(ev) {
    if (!CRM.BarStyle) {
        CRM.BarStyle = {};
        CRM.BarStyle.Right = CRM.Bar.css('right');
        CRM.BarStyle.Filter = CRM.Bar.css('filter');
    };
    $(this).clearQueue().animate({ right: 0, filter: 'none' });
};

function AsHoverItem(ev) {
    if (!CRM.BarStyle.BK) {
        CRM.BarStyle.BK = CRM.Bar.css('backgroundColor');
    };
    $(this).css('backgroundColor', 'whitesmoke');
};

function AsLeaveItem(ev) {
    $(this).css('backgroundColor', CRM.BarStyle.BK);
};

function AsLeave(ev) {
    $(this).clearQueue().css('filter', CRM.BarStyle.Filter).css('background-color', 'none').animate({ right: CRM.BarStyle.Right });
};